﻿using System;

namespace AutoNZBLoader
{
    public class FeedItem
    {
        private readonly string _Title;
        public string Title
        {
            get { return _Title; }
        } 

        private readonly string _Description;
        public string Description
        {
            get { return Description; }
        }

        private readonly string _Link;
        public string Link
        {
            get { return _Link; }
        }


        private readonly long _Season;
        public long Season
        {
            get { return _Season; }
        }

        private readonly long _Episode;
        public long Episode
        {
            get { return _Episode; }
        }

        private readonly bool _UseTitleAsFilename;
        public bool UseTitleAsFilename
        {
            get { return _UseTitleAsFilename; }
        }

        public FeedItem(string title, string description, string link, bool UseTitleAsFilename)
        {
            this._Title = title;
            this._Description = description;
            this._Link = link;
            this._UseTitleAsFilename = UseTitleAsFilename;

            //Get Season / Episode
            Util.TitleParser.Result r;
            r = Util.TitleParser.Parser.ParseTitle(_Title);
            if(r == null)
            {
                r = Util.TitleParser.Parser.ParseTitle(_Description);
                if (r == null)
                {
                    throw new ArgumentException("Couldn't get season/episode number.");
                }
            }
            _Season = r.Season;
            _Episode = r.Episode;
        }


        public void Download(Request request)
        {
            string filename;
            using (System.Data.IDbCommand cmd = Util.Database.DB.CreateCommand())
            {
                cmd.CommandText = "select value from property where name='NZB Download Folder'";
                filename = cmd.ExecuteScalar().ToString();
            }

            if (!filename.EndsWith("\\", StringComparison.OrdinalIgnoreCase)) filename += "\\";
            if (UseTitleAsFilename)
            {
                filename += Title.Replace(':', '_').Replace('?', '_').Replace('/', '_').Replace('*', '_').Replace('"', '_').Replace('|', '_').Replace('<', '_').Replace('>', '_');
                if (!filename.EndsWith(".nzb", true, System.Globalization.CultureInfo.InvariantCulture))
                {
                    filename += ".nzb";
                }
            }
            else
            {
                filename += string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} S{1:00}E{2:00}.nzb", request.Name.Replace(':', '_').Replace('?', '_').Replace('/', '_').Replace('*', '_').Replace('"', '_').Replace('|', '_').Replace('<', '_').Replace('>', '_'), Season, Episode);
            }

            try
            {
                Util.Net.Download.DownloadFile(Link, filename);
            }
            catch (Util.Net.DownloadException)
            {
                return;
            }
            request.MarkAsDownloaded(Season, Episode, Link);
        }
    }
}
